package com.xyz.jira.plugin.responses;

import java.util.ArrayList;
import java.util.Collection;

import javax.xml.bind.annotation.*;

import com.atlassian.jira.issue.issuetype.IssueType;

/**
 * A message returned from the {@link IssueTypes}
 */
@XmlRootElement(name = "issueTypes")
@XmlAccessorType(XmlAccessType.FIELD)
public class IssueTypesInfo {
	@XmlAttribute
	private String pid;

	@XmlElement(name = "issueTypesList")
	private Collection<IssueTypeItem> issueTypes = new ArrayList<IssueTypeItem>();

	public IssueTypesInfo() {
	}

	public IssueTypesInfo(String pid, Collection<IssueType> issueTypes) {
		this.pid = pid;
		this.setIssueTypes(issueTypes);
	}

	public String getPid() {
		return pid;
	}

	public void setPid(String pid) {
		this.pid = pid;
	}

	public Collection<IssueTypeItem> getIssueTypes() {
		return issueTypes;
	}

	public void setIssueTypes(Collection<IssueType> issueTypes) {
		if (issueTypes != null) {
			for (IssueType iType : issueTypes) {
				this.issueTypes.add(new IssueTypeItem(iType.getId(), iType
						.getName()));
			}
		} else {
			this.issueTypes.add(new IssueTypeItem("-1", "No issue types available"));
		}
	}

	@XmlRootElement(name = "issue")
	@XmlAccessorType(XmlAccessType.FIELD)
	private class IssueTypeItem {
		private String id;
		private String name;

		IssueTypeItem(String id, String name) {
			this.setId(id);
			this.setName(name);
		}

		@XmlElement(name = "id")
		public String getId() {
			return id;
		}

		public void setId(String id) {
			this.id = id;
		}

		@XmlElement(name = "name")
		public String getName() {
			return name;
		}

		public void setName(String name) {
			this.name = name;
		}
	}
}
